class MassDistributionCheckToolManager {
  constructor() {
    this.massAttributeInfo = {};//质量属性
    this.massValue = 0;//用于展示的质量值
    this.isCovMass = false;
    this.isCovPnt = false;
    this.instanceId = 0;
  }

  reset() {
    this.massAttributeInfo = {};//质量属性
  }

  parseMassAttributeInfos(attributeInfo) {
    let responseInfo = attributeInfo.massAttributaData;
    let massAttribueUnit = attributeInfo.massAttributeUnit;
    this.massAttributeInfo = {};
    this.isCovMass = responseInfo['CoverMassFlag'] || false;
    this.isCovPnt = responseInfo['CoverCentroidFlag'] || false;
    this.coverCoordinateId = responseInfo['CoverCoordinateId'] || 0;
    for (let attributeName in responseInfo) {
      if (responseInfo.hasOwnProperty(attributeName) && !['CoverMassFlag', 'CoverCentroidPt', 'CoverCoordinateId'].includes(attributeName)) {
        let unit = this.getUnitByName(attributeName, massAttribueUnit);
        if (attributeName === 'BaryCenter') {
          this.massAttributeInfo[attributeName] = {
            name: (this.isCovPnt ? 'Cov' : '') + attributeName,
            unit: unit,
            x: {
              name: 'x',
              value: this.isCovPnt ? this.setValue(unit, (displayPoint?.x || 0) / 10, attributeName) : this.setValue(unit, responseInfo[attributeName][0], attributeName, massAttribueUnit),
            },
            y: {
              name: 'y',
              value: this.isCovPnt ? this.setValue(unit, (displayPoint?.y || 0) / 10, attributeName) : this.setValue(unit, responseInfo[attributeName][1], attributeName, massAttribueUnit),
            },
            z: {
              name: 'z',
              value: this.isCovPnt ? this.setValue(unit, (displayPoint?.z || 0) / 10, attributeName) : this.setValue(unit, responseInfo[attributeName][2], attributeName, massAttribueUnit),
            },
          };
        } else if (['BT', 'OT'].includes(attributeName)) {
          let valList = [];
          responseInfo[attributeName].forEach(value => {
            valList.push(this.setValue(unit, value, attributeName, massAttribueUnit));
          });
          let [Lxx, Lxy, Lxz,
            Lyx, Lyy, Lyz,
            Lzx, Lzy, Lzz] = valList;
          this.massAttributeInfo[attributeName] = {
            name: attributeName,
            value: {
              Lxx, Lxy, Lxz,
              Lyx, Lyy, Lyz,
              Lzx, Lzy, Lzz,
            },
            unit: unit,
          };
        } else if (attributeName === 'T1') {
          let valList = [];
          responseInfo[attributeName].forEach(value => {
            valList.push(this.setValue(null, value, attributeName, massAttribueUnit, false));
          });
          let [Lx0, Lx1, Lx2,
            Ly0, Ly1, Ly2,
            Lz0, Lz1, Lz2] = valList;

          this.massAttributeInfo[attributeName] = {
            name: attributeName,
            value: {
              Lx0, Lx1, Lx2,
              Ly0, Ly1, Ly2,
              Lz0, Lz1, Lz2,
            },
            unit: unit,
          };
        } else if (attributeName === 'T2') {
          let valList = [];
          responseInfo[attributeName].forEach(value => {
            valList.push(this.setValue(unit, value, attributeName, massAttribueUnit));
          });
          let [Px, Py, Pz] = valList;
          this.massAttributeInfo[attributeName] = {
            name: attributeName,
            value: {Px, Py, Pz},
            unit: unit,
          };
        } else if (attributeName === 'Density') {
          let densityList = responseInfo[attributeName].sort();
          let hasDifferentDensity = densityList.some(value => {
            return value !== densityList[0];
          });
          this.massAttributeInfo[attributeName] = {
            name: attributeName,
            unit: unit,
            value: this.setValue(unit, densityList[0], attributeName, massAttribueUnit),
            different: hasDifferentDensity,
          };
        } else if (attributeName === 'Mass') {
          this.massAttributeInfo[attributeName] = {
            name: (this.isCovMass ? 'Cov' : '') + attributeName,
            unit: unit,
            value: this.setValue(unit, responseInfo[attributeName], attributeName, massAttribueUnit),
          };
          this.massValue = this.setValue(unit, responseInfo[attributeName], attributeName, massAttribueUnit);
        } else {
          if (attributeName === 'CoverCentroidPt') {
            //TODO 方便后续逻辑单独维护，目前暂无处理
          } else {
            this.massAttributeInfo[attributeName] = {
              name: attributeName,
              unit: unit,
              value: this.setValue(unit, responseInfo[attributeName], attributeName, massAttribueUnit),
            };
          }
        }
      }
    }
  }

  //获取对应的单位
  getUnitByName(name, massAttributeUnit) {
   if (name === 'Volume') {
      return massAttributeUnit.length + '³';
    }
    if (name === 'Area') {
      return massAttributeUnit.length + '²';
    }
    if (name === 'Mass') {
      return massAttributeUnit.weight;
    }
    if (name === 'Density') {
      return massAttributeUnit.densityUnit;
    }
  }

  //设置精度及科学计数后的数值
  setValue(unit, value, name, massAttributeUnit, allowScienceNotation = true) {
    let tempVal = this.getValueByUnit(unit, value);
    let precision = this.getValuePrecision(name, massAttributeUnit);
    if (name === 'Density') {
      let numReg = /[1-9]/;
      let valueStr = String(tempVal);
      if (numReg.test(valueStr)) {
        let index = numReg.exec(valueStr).index;
        if (valueStr.indexOf('.') !== -1) {
          precision = valueStr.indexOf('.') > index ? index : index - 1;
          if (typeof tempVal === 'number') {
            tempVal = tempVal.toFixed(precision);
          }
        }
      }
      return tempVal;
    }
    let n = precision; //!<保留三位小数
    let t = 1;
    for (; n > 0; t *= 10, n--) ;
    for (; n > 0; t /= 10, n++) ;
    // if (massAttributeSettingManager.scienceNotation && allowScienceNotation) {
    let scienceNotation = false;
    if (scienceNotation && allowScienceNotation) {
      if (tempVal > 0) {
        let p = Math.floor(Math.log(tempVal) / Math.LN10);
        if (p !== 0) {
          let n = tempVal * (Math.pow(10, -p));
          value = Math.round(n * t) / t + 'e+' + p;
        } else {
          value = Math.round(tempVal * t) / t;
        }
      } else if (tempVal < 0) {
        tempVal = -tempVal;
        let p = Math.floor(Math.log(tempVal) / Math.LN10);
        if (p !== 0) {
          let n = tempVal * Math.pow(10, -p);
          value = -Math.round(n * t) / t + 'e+' + p;
        } else {
          value = Math.round(tempVal * t) / t;
        }
      } else {
        value = 0;
      }
    } else {
      value = Math.round(tempVal * t) / t;
    }

    if (typeof value === 'number') {
      value = value.toFixed(precision);
    }

    return value;
  }
  //获取对应的精度
  getValuePrecision(name, massAttributeUnit) {
    if (name === 'Mass') {
      return massAttributeUnit.weightPrecision;
    } else {
      return massAttributeUnit.lengthPrecision;
    }
  }
  //根据单位换算数值
  getValueByUnit(unit, value) {
    if (unit === 'mm³') {
      value = value * 1000;
    }
    if (unit === 'm³') {
      value = value / 1000000;
    }
    if (unit === 'mm²') {
      value = value * 100;
    }
    if (unit === 'm²') {
      value = value / 10000;
    }
    if (unit === 'mm') {
      value = value * 10;
    }
    if (unit === 'm') {
      value = value / 100;
    }
    if (unit === 'g/mm³') {
      value = value / 1000;
    }
    if (unit === 'g/m³') {
      value = value * 1000000;
    }
    if (unit === 'g * mm²') {
      value = value * 100;
    }
    if (unit === 'g * m²') {
      value = value / 10000;
    }
    if (unit === 'kg/mm³') {
      value = value / 1000000;
    }
    if (unit === 'kg/m³') {
      value = value * 1000;
    }
    if (unit === 'kg * mm²') {
      value = value / 10;
    }
    if (unit === 'kg * m²') {
      value = value / 10000000;
    }
    if (unit === 'kg') {
      value = value / 1000;
    }
    return value;
  }
}
const massDistributionCheckToolManager = new MassDistributionCheckToolManager();
export { massDistributionCheckToolManager };
